package code.example.base.temp;

import code.example.tool.LogFactory;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.TimeUnit;

public class TestSocketServer {
    class TaskRunnable implements  Runnable{
        Socket socket = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;
        String hostName = null;
        byte [] pong = new byte []{'+','P','O','N','G'};
        byte [] nodes = "bb0203704fedd59ce568b89e9d2977259111494c 192.168.7.251:6379 master - 0 1521101560838 43 connected 0-16383".getBytes();
        public TaskRunnable(Socket socket){
            try {
                this.socket = socket;
                this.inputStream = socket.getInputStream();
                this.outputStream = socket.getOutputStream();
                this.hostName = socket.getInetAddress().getHostName();
                System.out.println(this.hostName + ":connected");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            byte [] data = new byte [1024];
            while(true){
                try {
                    int length = inputStream.read(data);
                    StringBuilder sb = new StringBuilder();
                    for(int i=0;i<length;i++){
                        String hexString  = Integer.toHexString(data[i]);
                        sb.append(hexString);
                    }
                    LogFactory.getLog().info(sb.toString());
                    String str = new String(data,0,length);
                    LogFactory.getLog().info(str);
                    if(str.indexOf("PING") != -1){
                        outputStream.write(pong);
                        outputStream.flush();
                    }else if(str.indexOf("CLUSTER") != -1 && str.indexOf("nodes") != -1){
                        /**
                         请求参数：2a32da2437da434c5553544552da2435da6e6f646573da
                         *2  //2个参数，后面跟回车换行
                         $7 //第一个长7位，后面跟回车换行
                         CLUSTER  //每二个参数内容，后面跟回车换行
                         $5 //第二个长5位，后面跟回车换行
                         nodes //每二个参数内容，后面跟回车换行

                         */
                        int nodesLength = nodes.length;
                        outputStream.write('$');//标志符
                        outputStream.write((nodesLength+"").getBytes());//长度
                        outputStream.write('\r');
                        outputStream.write('\n');
                        outputStream.write(nodes);
                        outputStream.write('\r');
                        outputStream.write('\n');
                        outputStream.flush();
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
    public void newThread(Socket socket){
        TaskRunnable taskRunnable = new TaskRunnable(socket);
        new Thread(taskRunnable).start();
    }
    public void start(){
        try {
            ServerSocket ss = new ServerSocket();
            ss.bind(new InetSocketAddress("127.0.0.1", 6379));
            while(true){
                Socket socket = ss.accept();
                newThread(socket);
            }
           // TimeUnit.MINUTES.sleep(10);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String [] args){
        new TestSocketServer().start();
    }
}
